package com.huizhonglm.diap.core.location;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.huizhonglm.diap.DiapApplication;
import com.huizhonglm.diap.core.config.Constants;
import com.huizhonglm.diap.core.storage.model.LocationInfo;
import com.huizhonglm.diap.core.utils.AMapUtil;
import com.huizhonglm.diap.core.utils.DeviceUtil;
import com.huizhonglm.diap.core.utils.LogUtil;

import java.util.Calendar;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2016-04-26 15:02
 */
public class LocationService extends Service implements AMapLocationListener {

    private AMapLocationClient client = null;
    private AMapLocationClientOption option = null;

    @Override
    public IBinder onBind(Intent intent) {
        LogUtil.print("locationService - onBind");
        return null;
    }

    @Override
    public void onCreate() {
        LogUtil.print("locationService - onCreate");
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        LogUtil.print("locationService - onStartCommand");
        activate();
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        LogUtil.print("locationService - onDestroy");
        super.onDestroy();
        deactivate();
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (aMapLocation.getErrorCode() == AMapLocation.LOCATION_SUCCESS) {
            LogUtil.print("locationService - " + AMapUtil.getLocationStr(aMapLocation));
            sendLocationBroadcast(saveLocationInfo(aMapLocation));
        } else {
            sendLocationBroadcast(null);
        }
    }

    private void sendLocationBroadcast(LocationInfo locInfo){
        LogUtil.print("locationService - sendLocationBroadcast");
        Intent sendIntent = new Intent(Constants.LOCATE_SERVICE_COMPLATE_ACTION);
        if (locInfo != null) {
            sendIntent.putExtra(Constants.LOCATE_SERVICE_LOCATION, locInfo);
        }
        sendBroadcast(sendIntent);
        stopSelf();
    }

    private void activate(){
        boolean isGpsOpen = DeviceUtil.GPSIsOpen(this);
        LogUtil.print("locationService - isGpsOpen=" + isGpsOpen);
        option = new AMapLocationClientOption();
        // 设置定位模式为高精度模式
        option.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        // 设置是否单次定位
        option.setOnceLocation(true);
        // 设置是否需要显示地址信息
        option.setNeedAddress(true);
        // 设置是否优先返回GPS定位结果，如果30秒内GPS没有返回定位结果则进行网络定位
        // 注意：只有在高精度模式下的单次定位有效，其他方式无效
        option.setGpsFirst(isGpsOpen);
        // 设置发送定位请求的时间间隔,最小值为1000，如果小于1000，按照1000算
        option.setInterval(1000);
        // 设置定位监听
        client = new AMapLocationClient(DiapApplication.getInstance().getContext());
        client.setLocationListener(this);
        // 设置定位参数
        client.setLocationOption(option);
        // 启动定位
        client.startLocation();
        LogUtil.print("locationService - startLocation");
    }

    /**
     * 如果AMapLocationClient是在当前Activity实例化的，
     * 在Activity的onDestroy中一定要执行deactivate
     */
    private void deactivate() {
        if (null != client) {
            client.stopLocation();
            client.onDestroy();
            client = null;
            option = null;
        }
    }

    /**
     * 保存定位的位置信息
     *
     * @param location 位置信息
     */
    private LocationInfo saveLocationInfo(AMapLocation location) {
        LocationInfo locInfo = new LocationInfo();
        locInfo.setLatitude(location.getLatitude());
        locInfo.setLongitude(location.getLongitude());
        locInfo.setProvince(location.getProvince());
        locInfo.setCity(location.getCity());
        locInfo.setDistrict(location.getDistrict());
        locInfo.setCityCode(location.getCityCode());
        locInfo.setAdCode(location.getAdCode());
        locInfo.setAddress(location.getAddress());
        locInfo.setCountry(location.getCountry());
        locInfo.setPoiName(location.getPoiName());
        locInfo.setStreet(location.getStreet());
        locInfo.setStreetNum(location.getStreetNum());
        locInfo.setAoiName(location.getAoiName());
        locInfo.setErrorCode(location.getErrorCode());
        locInfo.setErrorInfo(location.getErrorInfo());
        locInfo.setLocationDetail(location.getLocationDetail());
        locInfo.setLocationType(location.getLocationType());
        locInfo.setTime(Calendar.getInstance().getTimeInMillis());
        locInfo.setAdCode(location.getAdCode());
        // 保存经纬度
        LocationUtil.setLocation(locInfo);
        return locInfo;
    }
}
